package tienmanh.gs.goldminer.sprite;

import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.entity.modifier.LoopEntityModifier;
import org.andengine.entity.modifier.PathModifier;
import org.andengine.entity.modifier.PathModifier.Path;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.opengl.texture.region.ITiledTextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;

import tienmanh.gs.goldminer.manager.ResourcesManager;
import tienmanh.gs.goldminer.manager.SceneManager;
import tienmanh.gs.goldminer.scene.GameScene;
import tienmanh.gs.goldminer.util.Value;


public class Miner extends AnimatedSprite implements SensorEventListener
{
	public int xSpeed;
	public boolean checkRun ;
  TimerHandler check = new TimerHandler(0.8F, false, new ITimerCallback()
  {
    public void onTimePassed(TimerHandler paramTimerHandler)
    {
      Miner.this.getInstance().stopAnimation();
      Miner.this.getInstance().animate(new long[] { 200L, 200L }, 0, 1, 0);
    }
  });
  
  TimerHandler throwss = new TimerHandler(0.1F, false, new ITimerCallback()
  {
    public void onTimePassed(TimerHandler paramTimerHandler)
    {
    	 Miner.this.getInstance().stopAnimation();
         Miner.this.getInstance().animate(new long[] { 100L, 100L }, 0, 1, true);	
    }
  });
  
  private LoopEntityModifier lemRun;

  public Miner(float paramFloat1, float paramFloat2, float paramFloat3, float paramFloat4, ITiledTextureRegion paramITiledTextureRegion, VertexBufferObjectManager paramVertexBufferObjectManager)
  {
    super(paramFloat1, paramFloat2, paramFloat3, paramFloat4, paramITiledTextureRegion, paramVertexBufferObjectManager);
    checkRun = true;
    xSpeed=1;
    initSensor();
//    animate(new long[]{100,100}, new int[]{0,1}, true);
//    if(GameScene.getInstace().level < 11){
//    	  setCurrentTileIndex(0);
//    }
  }

  public Miner getInstance()
  {
    return this;
  }

  public void minerRun()
  {
     setPosition(getX()+xSpeed, getY());
  }

  protected void onManagedUpdate(float paramFloat)
  {
    super.onManagedUpdate(paramFloat);
//    if(checkRun && GameScene.isMinerRunning){
//    if (getX() <= 20.0F)
//    {
//    	xSpeed = -xSpeed;
//    }
//    else
//    if(getX() >= 700.0F - getWidth()){
//    	xSpeed = -xSpeed;
//    }
//    minerRun();
//    }
  }

//  public void setAnimatePower()
//  {
//    stopAnimation();
//    animate(new long[] { 200L, 200L }, 21, 22, 2);
//    this.check.reset();
//    unregisterUpdateHandler(this.check);
//    registerUpdateHandler(this.check);
//  }
  //
  
  public void setAnimateThrow()
  {
    animate(new long[] { 200L, 200L}, 0, 1, true);
  }
  
  public void unAnimateThrow()
  {
    stopAnimation();
//    unregisterUpdateHandler(this.throwss);
  }

//  public void setAnimatePull(float paramFloat)
//  {
//    int i;
//    int k;
//    int j;
//    if (paramFloat >= new Value().SPEED_GOLD_2)
//    {
//      i = 300;
//      k = 7;
//      j = 13;
//    }
//    else{
//    	i = 200;
//        j = 6;
//        k = 0;
//    }
//      stopAnimation();
//      long[] arrayOfLong = new long[7];
//      arrayOfLong[0] = i;
//      arrayOfLong[1] = i;
//      arrayOfLong[2] = i;
//      arrayOfLong[3] = i;
//      arrayOfLong[4] = i;
//      arrayOfLong[5] = i;
//      arrayOfLong[6] = i;
//      animate(arrayOfLong, k, j, true);
//  }

  public void unMinerRun()
  {
	  checkRun = false;
  }
  public void MinerRun()
  {
	  checkRun = true;
  }
  
  private SensorManager sensorManager;
  public void initSensor(){
	  sensorManager = (SensorManager) ResourcesManager.getInstance().activity.getSystemService(ResourcesManager.getInstance().activity.SENSOR_SERVICE);
      sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), sensorManager.SENSOR_DELAY_GAME);
  }

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
	// TODO Auto-generated method stub
	
}

@Override
public void onSensorChanged(SensorEvent event) {
	// TODO Auto-generated method stub
	synchronized (this) {
        switch (event.sensor.getType()) {
            case Sensor.TYPE_ACCELEROMETER:
            	int valueX = (int)event.values[1];
            	if(checkRun){
            		if(getX() >= 200 && getX() <= 600)
               setPosition(getX()+valueX, getY());
            		else 
                		if(getX() < 200) setPosition(200, getY());
                		if(getX() > 600) setPosition(600, getY());
            	}
            		
            		
                break;
        }
    }
}
}
